package net.madarco.phpsense.util;

import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

public class EclipseConsole {

	private static final String CONSOLE_NAME = "net.madarco.phpsense.console";

	private MessageConsole findConsole(String name) {
		ConsolePlugin plugin = ConsolePlugin.getDefault();
		IConsoleManager conMan = plugin.getConsoleManager();
		IConsole[] existing = conMan.getConsoles();
		for (int i = 0; i < existing.length; i++) {
			if (name.equals(existing[i].getName())) {
				return (MessageConsole) existing[i];
			}
		}

		// no console found, so create a new one
		MessageConsole myConsole = new MessageConsole(name, null);
		conMan.addConsoles(new IConsole[] { myConsole });
		return myConsole;
	}

	public MessageConsoleStream getConsoleOutputSteam() {
		MessageConsole myConsole = this.findConsole(CONSOLE_NAME);
		MessageConsoleStream out = myConsole.newMessageStream();
		return out;
	}
	
	public void trace(String msg) {
		getConsoleOutputSteam().println("PhpSense: "  + msg);
	}
	
	public void error(String msg) {
		getConsoleOutputSteam().println("PhpSense ERROR: "  + msg);
	}
}
